import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const user = (() => {
  try {
    Vue.prototype.place = JSON.parse(localStorage.place)
    return JSON.parse(localStorage.user)
  } catch (e) {
    return {}
  }
})()

export default new Vuex.Store({
  state: {
    user,
    page: {
      count: 1,
      cur: 1
    }
  },
  mutations: {
    update_user(state, user) {
      state.user = user
      try {
        localStorage.user = JSON.stringify(user)
      } catch (e) {
        localStorage.user = ''
      }
    },
    update_page(state, page) {
      state.page = page
    }
  },
  actions: {
    update_user({ commit }, user) {
      commit('update_user', user)
    },
    update_page({ commit }, page) {
      commit('update_page', page)
    }
  }
})